﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using System.Collections.ObjectModel;

namespace HM_Viewer_Silverlight
{
    public partial class CurrentsTables : Page
    {
        private HMServiceReference1.Service1Client cl;
        public HMServiceReference1.players currentplayer;
        public CurrentsTables()
        {
            InitializeComponent();

            Loaded +=new RoutedEventHandler(CurrentsTables_Loaded);

            comboBox_juoeurs.DataContext = new List<HMServiceReference1.players>();

        }


        void  CurrentsTables_Loaded(object sender, RoutedEventArgs e)
        {

            //on définit le client
            cl = new HMServiceReference1.Service1Client();

            //on définit les méthodes de récupèrattion des données

            //liste des sites web
            cl.GetPokerSitesCompleted += new EventHandler<HMServiceReference1.GetPokerSitesCompletedEventArgs>(cl_GetPokerSitesCompleted);
            cl.GetPokerSitesAsync();    //on lance la méthode

            this.comboBox_Sites.DataContext = new ObservableCollection<HMServiceReference1.pokersites>();


        }



        /// <summary>
        /// on traite le retour
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void cl_GetPokerSitesCompleted(object sender, HMServiceReference1.GetPokerSitesCompletedEventArgs e)
        {
            comboBox_Sites.ItemsSource = e.Result.ToList();
            comboBox_Sites.SelectedIndex = comboBox_Sites.Items.Count - 2;

        }

        


        // S'exécute lorsque l'utilisateur navigue vers cette page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void comboBox_Sites_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            //sur changement de site on rafraichir la liste des joeurs
            cl.GetPokerSitePlayersCompleted += new EventHandler<HMServiceReference1.GetPokerSitePlayersCompletedEventArgs>(cl_GetPokerSitePlayersCompleted);
            int idsite = ((HMServiceReference1.pokersites)(comboBox_Sites.SelectedValue)).site_id;
            cl.GetPokerSitePlayersAsync(idsite);
        }

        void cl_GetPokerSitePlayersCompleted(object sender, HMServiceReference1.GetPokerSitePlayersCompletedEventArgs e)
        {

            comboBox_juoeurs.ItemsSource = e.Result.OrderByDescending(p => p.cashhands).ToList();
        }

        private void comboBox_juoeurs_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //getting the hand of the selected player
            cl.GetHandsActiveHandsCompleted += new EventHandler<HMServiceReference1.GetHandsActiveHandsCompletedEventArgs>(cl_GetHandsActiveHandsCompleted);
            int idPalyer = ((HMServiceReference1.players)(comboBox_juoeurs.SelectedValue)).player_id;
            cl.GetHandsActiveHandsAsync(idPalyer);

            
        }

        void cl_GetHandsActiveHandsCompleted(object sender, HMServiceReference1.GetHandsActiveHandsCompletedEventArgs e)
        {
            ObservableCollection<HandsCustomClass> hands= new ObservableCollection<HandsCustomClass>();

            foreach (HMServiceReference1.playerhandscashkeycolumns  item in e.Result)
	        {
                HandsCustomClass h = new HandsCustomClass();
                h.HandTime = item.handtimestamp;
                h.Rake = item.rakeamount;
                h.AmountWon = item.netamountwon;
                h.Card1 = item.holecard1int;
                h.Card2 = item.holecard2int;


                hands.Add(h);
	        }

            datagridhands.ItemsSource = hands;
        }

       

    }
}
